home *** CD-ROM | disk | FTP | other *** search
/ The Best of Down Under Games / The Best of Down Under Games.iso / 3dfx Screen Savers / Impact / IMPSRC.ZIP / LAND.CPP < prev    next >
C/C++ Source or Header  |  1997-07-24  |  15KB  |  471 lines

  1. #include "winstuff.h"
  2. #include "land.h"
  3. #include "trigs.h"
  4. #include "things.h"
  5. #include "stdio.h"
  6. #include <math.h>
  7. char MyS[256];
  8. /*const int vertsep = 1024;
  9. const int tdx = 4,tdz = 4;
  10. const int gridsep = vertsep*(tdx);
  11. const int GridNum = 32;*/
  12. int frameCount;
  13. float map[specwidth][specwidth];
  14. entity_t *Self, *Other, *Head;
  15. land_t::land_t(){
  16.     //Start = Trans = 0;
  17.     //Rects = Water = 0;
  18.     //Water = 0;
  19.     id = -1;
  20.     minx = 0;
  21.     minz = 0;
  22.     maxx = 0;
  23.     maxz = 0;
  24.     centerx = 0;
  25.     centerz = 0;
  26.     numland = 0;
  27.     numwater = 0;
  28.     NumLand++;
  29. }
  30. land_t::land_t(float x, float z, int mid){
  31.     generate(x,z,mid);
  32.     NumLand++;
  33. }
  34. land_t::~land_t(){
  35.     Print("destructor");
  36.     return;
  37.     //if(Start) delete Start;
  38.     //if(Trans) delete Trans;
  39.     //if(Rects) delete Rects;
  40.     //if(Water) delete Water;
  41.     //NumLand--;
  42. }
  43. land_t::render(Matrix rot, float x, float y, float z){
  44.     int render = 0,Realr=0,gridsep2 = gridsep/2,gridsep3 = gridsep*2;//,gridsep3 = gridsep2;//<<1;
  45.     int cnt=4,cnt2=0,hitthat;
  46.         int i,j,k,l;
  47.     float x1,y1,z1;
  48.     GrVertex Cen;
  49.     Matrix yawcam;
  50.     // Camzoomx,Camzoomy;
  51.     rendered = render;
  52.     if(x < minx-gridsep2 || x > maxx+gridsep2 || z < minz-gridsep2 || z > maxz+gridsep2){
  53.         for(cnt = 0;cnt<5 && render == 0;cnt++){
  54.             x1 = pntx[cnt];// - x;
  55.             z1 = pntz[cnt];// - z;
  56.             if(x1*Tridz[0]+z1*Tridx[0]<Trib[0] &&
  57.             x1*Tridz[1]+z1*Tridx[1]<Trib[1] &&
  58.             x1*Tridz[2]+z1*Tridx[2]<Trib[2])
  59.                 render = 1;
  60.         }
  61.  
  62.     }
  63.     else
  64.         render = 2;
  65.  
  66.     rendered = render;
  67.  
  68.     if (render){
  69.         if(!Start) return 0;
  70.         //cnt = firstreal;
  71.         //cnt2=0;
  72.  
  73.         for(cnt = 0;cnt<vertscnt;cnt++){
  74.             hitthat = MyVerts[cnt];
  75.             if(Start[hitthat].oow <GameTime){
  76.                 Trans[hitthat].x = Start[hitthat].x - x;
  77.                 Trans[hitthat].y = Start[hitthat].y - y;
  78.                 Trans[hitthat].z = Start[hitthat].z - z;
  79.  
  80.                 //Trans[cnt].a=255;
  81.                 //Trans[hitthat].r = Start[hitthat].r;
  82.                 //Trans[hitthat].g = Start[hitthat].g;
  83.                 //Trans[hitthat].b = Start[hitthat].b;
  84.  
  85.                 lit[hitthat].r = .75;
  86.                 lit[hitthat].g = .75;
  87.                 lit[hitthat].b = .75;
  88.  
  89.                 PointMatMult(&Trans[hitthat],&Trans[hitthat],rot);
  90.  
  91.                 Trans[hitthat].ooz =
  92.                 Trans[hitthat].oow = 1/Trans[hitthat].z;
  93.                 Trans[hitthat].x = Camzoomx*Trans[hitthat].x*Trans[hitthat].ooz+wHWidth+vertex_snapper;
  94.                 Trans[hitthat].y = Camzoomy*Trans[hitthat].y*Trans[hitthat].ooz+wHHeight+vertex_snapper;
  95.                 Trans[hitthat].x -=vertex_snapper;
  96.                 Trans[hitthat].y -=vertex_snapper;
  97.             }
  98.         }
  99.         Other = Head;
  100.         while(Other){
  101.             if(Other->light && Other->x > minx-gridsep3 && Other->x < maxx+gridsep3 && Other->z > minz-gridsep3 && Other->z < maxz+gridsep3)
  102.                 castlight(Other);
  103.             Other = Other->next;
  104.         }
  105.  
  106.         for(cnt = 0;cnt<vertscnt;cnt++){
  107.             hitthat = MyVerts[cnt];
  108.             if(Start[hitthat].oow < GameTime){
  109.                 Trans[hitthat].r = Start[hitthat].r*lit[hitthat].r;
  110.                 Trans[hitthat].g = Start[hitthat].g*lit[hitthat].g;
  111.                 Trans[hitthat].b = Start[hitthat].b*lit[hitthat].b;
  112.  
  113.                 if(Trans[hitthat].r>255) Trans[hitthat].r = 255;
  114.                 if(Trans[hitthat].g>255) Trans[hitthat].g = 255;
  115.                 if(Trans[hitthat].b>255) Trans[hitthat].b = 255;
  116.                 Start[hitthat].oow=GameTime;
  117.             }
  118.         }
  119.  
  120.  
  121.         //int tmp,xbig=0,ybig=0,xsml=0,ysml=0,hitthat;
  122.         for(cnt=0;cnt<sqrcnt;cnt++){
  123.             hitthat = MyRects[cnt];
  124.             i = Rects[hitthat].corn[0];
  125.             j = Rects[hitthat].corn[1];
  126.             k = Rects[hitthat].corn[2];
  127.             l = Rects[hitthat].corn[3];
  128.             AttCount+=2;
  129.             int back1=2,back2=2;
  130.             //Backface removal
  131.             if( Trans[i].z < 1 &&
  132.                 Trans[j].z < 1 &&
  133.                 Trans[k].z < 1 &&
  134.                 Trans[l].z < 1)
  135.                     continue;
  136.             if(
  137.                 Trans[i].z >= 1 &&
  138.                 Trans[j].z >= 1 &&
  139.                 Trans[k].z >= 1 &&
  140.                 Trans[l].z >= 1){
  141.  
  142.                 back1=(((Trans[j].x - Trans[i].x)*
  143.                 (Trans[k].y - Trans[i].y)-
  144.                 (Trans[j].y - Trans[i].y)*
  145.                 (Trans[k].x - Trans[i].x))>0);
  146.  
  147.                 back2=(((Trans[l].x - Trans[k].x)*
  148.                 (Trans[i].y - Trans[k].y)-
  149.                 (Trans[l].y - Trans[k].y)*
  150.                 (Trans[i].x - Trans[k].x))>0);
  151.  
  152.                 if(!back1 && !back2) continue;
  153.             }
  154.  
  155.             if(Trans[i].z < 1 || Trans[k].z < 1) {
  156.                 if(back1)
  157.                 MyDrawTriangleTooClose(Trans[i], Trans[j],Trans[k],
  158.                        Start[i],Start[j],Start[k],
  159.                        rot, x, y, z);
  160.                 if(back2)
  161.                 MyDrawTriangleTooClose(Trans[k], Trans[l],Trans[i],
  162.                        Start[k],Start[l],Start[i],
  163.                        rot, x, y, z);
  164.             }
  165.             else if(Trans[j].z < 1) {
  166.                 if(back1)
  167.                 MyDrawTriangleTooClose(Trans[i], Trans[j],Trans[k],
  168.                        Start[i],Start[j],Start[k],
  169.                        rot, x, y, z);
  170.                 /*if(Trans[l].z < 1)
  171.                        MyDrawTriangleTooClose(Trans[k], Trans[l],Trans[i],
  172.                            Start[k],Start[l],Start[i];
  173.                            rot, x, y, z);
  174.                 else*/
  175.                 if(back2)
  176.                     guDrawTriangleWithClip(&Trans[k],&Trans[l],&Trans[i]);
  177.  
  178.  
  179.             }
  180.             else if(Trans[l].z < 1) {
  181.                 if(back1)
  182.                 guDrawTriangleWithClip(&Trans[i],&Trans[j],&Trans[k]);
  183.                 if(back2)
  184.                 MyDrawTriangleTooClose(Trans[k], Trans[l],Trans[i],
  185.                        Start[k],Start[l],Start[i],
  186.                        rot, x, y, z);
  187.             }
  188.             else{
  189.  
  190.                 if(back1)
  191.                 guDrawTriangleWithClip(&Trans[i],&Trans[j],&Trans[k]);
  192.                 if(back2)
  193.                 guDrawTriangleWithClip(&Trans[k],&Trans[l],&Trans[i]);
  194. /*                if(back1)
  195.                 grDrawTriangle(&Trans[i],&Trans[j],&Trans[k]);
  196.                 if(back2)
  197.                 grDrawTriangle(&Trans[k],&Trans[l],&Trans[i]);*/
  198.  
  199.             }
  200.             TryCount+=2;
  201.             //if(TryCount>600*frameCount) {delete Trans;return 0;}
  202.  
  203.         }
  204.         //delete Trans;
  205.     }
  206.     return 1;
  207. }
  208.  
  209. void land_t::castlight(entity_t *Other){
  210.     int tmp,rgb, cnt = vertscnt,hitthat;
  211.     float dist,dist2,rdx,rdy,rdz,inten,al2=Other->lighta*Other->lighth;
  212.     al2*=al2;
  213.     if(Other->light == 0) return;
  214.     for(tmp=0;tmp<cnt;tmp++){
  215.         hitthat = MyVerts[tmp];
  216.         if(Start[hitthat].oow <GameTime){
  217.             rdx = Other->x-Start[hitthat].x;
  218.             rdy = Other->y-Start[hitthat].y;
  219.             rdz = Other->z-Start[hitthat].z;
  220.             dist2 = rdx*rdx+rdy*rdy+rdz*rdz;
  221.             if(dist2>al2) continue;
  222.             dist= sqrt(dist2);
  223.             inten = Other->lighth-dist/Other->lighta;//(dist2/382.5-5*dist/3+255)/255;//((dist2/255-dist+255)/255);
  224.             lit[hitthat].r+=(Other->lightr*inten)/255;
  225.             lit[hitthat].g+=(Other->lightg*inten)/255;
  226.             lit[hitthat].b+=(Other->lightb*inten)/255;
  227.             //if(lit[hitthat].r>2) lit[hitthat].r     = 2;
  228.             //if(lit[hitthat].g>2) lit[hitthat].g = 2;
  229.             //if(lit[hitthat].b>2) lit[hitthat].b = 2;
  230.             if(lit[hitthat].r<0) lit[hitthat].r = 0;
  231.             if(lit[hitthat].g<0) lit[hitthat].g = 0;
  232.             if(lit[hitthat].b<0) lit[hitthat].b = 0;
  233.         }
  234.     }
  235. }
  236.  
  237.  
  238. land_t::generate(float x, float z, int mid){
  239.     float dx,dz;
  240.     //int tdx=32,tdz = 32;
  241.     int hitx,hitz,cnt=0,clr,realx,realz;
  242.     int rect=tdx*tdz,mrec;
  243.     int colr,colg,colb,crn;
  244.     id = mid;
  245.     //Rects = new landrect_t[rect];
  246.     crn = 0;
  247.     minx = x;
  248.     minz = z;
  249.     maxx = x+gridsep;//+vertsep;
  250.     maxz = z+gridsep;//+vertsep;
  251.     pntx[4] = centerx = (minx+maxx)/2;
  252.     pntz[4] = centerz = (minz+maxz)/2;
  253.  
  254.     pntx[0] = pntx[1] = minx;
  255.     pntx[2] = pntx[3] = maxx;
  256.  
  257.     pntz[0] = pntz[2] = minz;
  258.     pntz[1] = pntz[3] = maxz;
  259.     pnty[0] = pnty[1] = pnty[2] = pnty[3] = pnty[4] = 0;
  260.  
  261.  
  262.     numland = rect;
  263.     numwater = 0;
  264.     mrec = 0;
  265.     for(cnt = 0;cnt<GridNum2*tdx*tdz;cnt++){
  266.         if(Rects[cnt].CentX > minx && Rects[cnt].CentX < maxx &&
  267.         Rects[cnt].CentZ > minz && Rects[cnt].CentZ < maxz){
  268.             MyRects[mrec] = cnt;
  269.             mrec++;
  270.             //if(mrec == sqrcnt)
  271.             //    break;
  272.         }
  273.  
  274.     }
  275.     //sprintf(MyS,"%i %i",mrec,sqrcnt);
  276.     //Print(MyS);
  277.     mrec = 0;
  278.     for(cnt = 0;cnt<width2;cnt++){
  279.         if(Start[cnt].x >= minx && Start[cnt].x <= maxx &&
  280.         Start[cnt].z >= minz && Start[cnt].z <= maxz){
  281.             MyVerts[mrec] = cnt;
  282.             mrec++;
  283.             if(mrec == vertscnt)
  284.                 break;
  285.         }
  286.     }
  287.  
  288.     return 1;
  289. }
  290. float Rand1(float base,int delta){
  291.     float i;
  292.     i=base+((random(2048))%delta)-(delta/2);
  293.     /*if (i<-128) i=-128; if (i>127) i=127;*/
  294.     return i;
  295. }
  296.  
  297. void DoOffsetSquare(int width){
  298.     int row_offset = 0;  // start at zero for first row
  299.  
  300.     /* Get the memory (widthxwidth bytes) */
  301.     //map = new int[width][width];
  302.     /* base value = 0, byte values are -128..127 */
  303.     for (int i=0;i<width;i++) for (int j=0;j<width;j++) map[i][j]=random(256)-128;
  304.     for (int l = 0;l<1;l++) {
  305.         map[0][0] = random(128);
  306.         map[0][width-1] = random(128);
  307.         map[width-1][0] = random(128);
  308.         map[width-1][width-1] = random(128);
  309.     }
  310.     for ( int square_size = width; square_size > 1; square_size /= 2 ){
  311.         int random_range = square_size;
  312.         for ( int x1 = row_offset; x1 < width; x1 += square_size ){
  313.             for ( int y1 = row_offset; y1 < width; y1 += square_size ){
  314.                 // Get the four corner points.
  315.  
  316.                 int x2 = (x1 + square_size) % width;
  317.                 int y2 = (y1 + square_size) % width;
  318.  
  319.                 float i1 = map[x1][y1];
  320.                 float i2 = map[x2][y1];
  321.                 float i3 = map[x1][y2];
  322.                 float i4 = map[x2][y2];
  323.  
  324.                 // Obtain new points by averaging the corner points.
  325.  
  326.                 float p1 = ((i1 * 9) + (i2 * 3) + (i3 * 3) + (i4)) / 16;
  327.                 float p2 = ((i1 * 3) + (i2 * 9) + (i3) + (i4 * 3)) / 16;
  328.                 float p3 = ((i1 * 3) + (i2) + (i3 * 9) + (i4 * 3)) / 16;
  329.                 float p4 = ((i1) + (i2 * 3) + (i3 * 3) + (i4 * 9)) / 16;
  330.  
  331.                 // Add a random offset to each new point.
  332.  
  333.                 p1 = Rand1(p1, random_range);
  334.                 p2 = Rand1(p2, random_range);
  335.                 p3 = Rand1(p3, random_range);
  336.                 p4 = Rand1(p4, random_range);
  337.  
  338.                 // Write out the generated points.
  339.  
  340.                 int x3 = (x1 + square_size/4) % width;
  341.                 int y3 = (y1 + square_size/4) % width;
  342.                 x2 = (x3 + square_size/2) % width;
  343.                 y2 = (y3 + square_size/2) % width;
  344.  
  345.                 map [x3][y3]   = p1;
  346.                 map [x2][y3]   = p2;
  347.                 map [x3][y2]   = p3;
  348.                 map [x2][y2]   = p4;
  349.             }
  350.         }
  351.         row_offset = square_size/4;  // set offset for next row
  352.     }
  353.     float miny=map[0][0],maxy=map[0][0];
  354.     for (i=0;i<width;i++)
  355.         for (int j=0;j<width;j++) {
  356.             if(map[i][j]>maxy) maxy = map[i][j];
  357.             if(map[i][j]<miny) miny = map[i][j];
  358.         }
  359.     float difx = 2;//0;//256 / (maxy-miny);
  360.     //float difn = -256/miny;
  361.     sprintf(MyS,"%f %f %f",map[0][0],maxy,miny);
  362.     Print(MyS);
  363.     for (i=0;i<width;i++)
  364.         for (int j=0;j<width;j++){
  365.             map[i][j] = (map[i][j])*difx;
  366.             if((i == 0 || i == ::width-1 || j == 0 || j == ::width -1) && map[i][j]<1024)
  367.                 map[i][j] = 1024;
  368.             if((i == 1 || i == ::width-2 || j == 1 || j == ::width -2) && map[i][j]<196)
  369.                 map[i][j] = 196;
  370.             if((i == 2 || i == ::width-3 || j == 2 || j == ::width -3) && map[i][j]<16)
  371.                 map[i][j] = 16;
  372.         }
  373.       //sprintf(MyS,"%f %f",map[0][0],maxy*difx-(maxy-miny));
  374.     //Print(MyS);
  375.     for (i=0;i<width;i++)
  376.         for (int j=0;j<width;j++) {
  377.             if(map[i][j]>maxy) maxy = map[i][j];
  378.             if(map[i][j]<miny) miny = map[i][j];
  379.         }
  380.     //sprintf(MyS,"%f %f %f",map[0][0],maxy,miny);
  381.     //Print(MyS);
  382.  
  383. }
  384.  
  385.  
  386.  
  387. void Genland(void){
  388.     int dx=GridNum,dz=GridNum,cx,cz,cnt=0;
  389.  
  390.     DoOffsetSquare(specwidth);
  391.  
  392.     int hitx,hitz,colg,colr,colb;
  393.     for(dz = 0;dz<width;dz++){
  394.         for(dx = 0;dx<width;dx++){
  395.             Start[cnt].x = dx*vertsep;
  396.             Start[cnt].z = dz*vertsep;
  397.             Start[cnt].y = map[dx][dz];
  398.  
  399.             colg = Start[cnt].y;
  400.             if(colg > 255) colg = 255;
  401.             if(colg < 0) colg = 0;
  402.  
  403.             colb = Start[cnt].y;
  404.             if(colb > 192)
  405.                 colb = 4*colb - 768;
  406.             else if(colb>0 && colb<193)
  407.                 colb = 0;
  408.             else if(colb <= 0) colb = -colb;
  409.  
  410.             if(colb>255) colb = 255;
  411.             if(colb<0) colb = 255;
  412.  
  413.             colr = Start[cnt].y;
  414.             if(colr > 192)
  415.                 colr = 4*colr - 768;
  416.             else
  417.                 colr = 5;
  418.             if(colr>255) colr = 255;
  419.             if(colr<32) colr = 32;
  420.  
  421.             if(colg <32)
  422.                 colg = 32;
  423.             if(colb < 32) colb = 32;
  424.             if(colr < 32) colr = 32;
  425.             Start[cnt].r = colr;
  426.             Start[cnt].g = colg;
  427.             Start[cnt].b = colb;
  428.             Start[cnt].a = 255;
  429.             Trans[cnt].a = 255;
  430.             cnt++;
  431.         }
  432.     }
  433.  
  434.     int crn = 0;
  435.     for(cnt = 0;cnt<Bigsqrcnt;cnt++){
  436.         Rects[cnt].corn[0] = crn;
  437.         Rects[cnt].corn[1] = crn+1;
  438.         Rects[cnt].corn[2] = crn+width+1;
  439.         Rects[cnt].corn[3] = crn+width;
  440.         Rects[cnt].CentX = (
  441.             Start[Rects[cnt].corn[0]].x+
  442.             Start[Rects[cnt].corn[1]].x+
  443.             Start[Rects[cnt].corn[2]].x+
  444.             Start[Rects[cnt].corn[3]].x
  445.             )/4;
  446.         Rects[cnt].CentZ = (
  447.             Start[Rects[cnt].corn[0]].z+
  448.             Start[Rects[cnt].corn[1]].z+
  449.             Start[Rects[cnt].corn[2]].z+
  450.             Start[Rects[cnt].corn[3]].z
  451.             )/4;
  452.         //sprintf(MyS,"%i %i",Rects[cnt].CentX ,Rects[cnt].CentZ );
  453.         //Print(MyS);
  454.         crn++;
  455.         if((crn+1)%(width)==0)
  456.             crn++;
  457.     }//Find the real one and replace it.
  458.  
  459.     dx=GridNum;
  460.     dz=GridNum;
  461.     cnt = 0;
  462.     for(cx = 0;cx<dx;cx++){
  463.         for(cz = 0;cz<dz;cz++){
  464.             land[cnt].generate(cx*gridsep,cz*gridsep,cnt);
  465.             cnt++;
  466.         }
  467.     }
  468. /*    for(cx = 0;cx<width;cx++)
  469.         delete map[cx];
  470.     delete *map;*/
  471. }